package com.nativetalk.controller.app.three.teacher.live;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.three.live.search.ThreeLiveSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeTeacherLiveController {
    private static Logger logger = Logger.getLogger(ThreeTeacherLiveController.class);
    @Resource
    private ThreeLiveSearchService threeLiveSearchService;

    /**
     * @return RetInfo
     * @Purpose 查找老师直播课程列表
     * @version 3.0
     * @time 2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/teacher/live/his/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo hisList(@RequestBody Page page,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info(retInfo.toString());
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("100");
                retInfo.setTip("Parameter token  is required");
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + retInfo);
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            page.setObj(tdNtTeacherResult.getTea_id());
            RetInfo retInfo = threeLiveSearchService.findLiveHisByTeaId(page, tdNtTeacherResult);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

}


